{% extends '../_manage.html' %}

{% block title %} Edit Wiki Page {% endblock %}

{% block head %}
<script>

const id = parseInt('{{ id }}');

$(() => {
    doAsync(
        async () => {
            let wp = await getJson(`/api/wikiPages/${id}`);
            let wiki = await getJson(`/api/wikis/${wp.wikiId}`);
            wp.publishAtLocal = timestampToDateTimeLocal(wp.publishAt);
            return {
                wp: wp,
                wiki: wiki
            };
        },
    	data => initVM(data),
    	err => UIkit.modal.alert(translateError(err))
    );
});

function initVM(data) {
    window.vm = new Vue({
        el: '#vm',
        data: {
        	wiki: data.wiki,
            wikiPage: data.wp
        },
        created: function () {
            $('#loading').hide();
            $('#vm').show();
        },
        methods: {
            submit: async function(event) {
                event.preventDefault();
                clearFormError();
                this.wikiPage.publishAt = dateTimeLocalToTimestamp(this.wikiPage.publishAtLocal);
                this.wikiPage.content = $('#content-editor').val();
                try {
                    await postJson(`/api/wikiPages/${this.wikiPage.id}`, this.wikiPage);
                    this.saved = true;
                    location.assign(`/manage/wiki/wiki_tree?id=${this.wikiPage.wikiId}`);
                } catch (err) {
                    UIkit.modal.alert(translateError(err));
                }
            }
        }
    });

    // check content when unload:
    window.onbeforeunload = function() {
        if (window.vm.saved) {
            return;
        }
        if (window.vm.wikiPage.content !== $('#content-editor').val()) {
            return "{{ _('Content has been changed.') }}";
        }
    };

    $('#content-editor').val(window.vm.wikiPage.content || '');
    var htmleditor = UIkit.htmleditor($('#content-editor').get(0), {
        markdown: true,
        maxsplitsize: 600
    });
    // override image action:
    htmleditor.off('action.image');
    htmleditor.on('action.image', function() {
        uploadImage(function (err, result) {
            if (err) {
                return;
            }
            htmleditor['replaceSelection']('\n![' + result.name + '](' + result.url + ')\n');
        });
    });
    window.htmleditor = htmleditor;
}
</script>

{% endblock %}

{% block main %}

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> {{ _('Loading') }}...
    </div>

    <div id="vm" class="uk-width-1-1">
        <ul class="uk-breadcrumb">
            <li><a href="/manage/wiki/">{{ _('All Wikis') }}</a></li>
            <li><a v-bind:href="'/manage/wiki/wiki_tree?id='+wiki.id" v-text="wiki.name"></a></li>
            <li class="uk-active"><span v-text="wikiPage.name"></span></li>
        </ul>

        <form v-on:submit.prevent="submit" class="uk-form uk-form-stacked uk-margin">
            <legend>{{ _('Edit Wiki Page') }}</legend>
            <fieldset>
                <div class="uk-alert uk-alert-danger uk-hidden"></div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Name') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model="wikiPage.name" type="text" maxlength="100" class="uk-width-1-1" placeholder="name">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="control-label">Publish At:</label>
                    <div class="controls">
                        <input v-model="wikiPage.publishAtLocal" name="publishAtLocal" type="datetime-local">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="control-label">Content:</label>
                    <div class="controls">
                        <textarea id="content-editor" rows="20" style="width:100%; resize:none;"></textarea>
                    </div>
                </div>
                <div class="uk-form-row">
                    <div class="controls">
                        <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-check"></i> {{ _('Save') }}</button>
                        <button type="button" class="uk-button" onclick="location.assign('/manage/wiki/wiki_tree?id=' + window.vm.wikiPage.wikiId)"><i class="uk-icon-times"></i> {{ _('Cancel') }}</button>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>

{% endblock %}
